#ifndef QNOTEBOOKLISTITEMWIDGET_H
#define QNOTEBOOKLISTITEMWIDGET_H

#include <MWidget>
#include "ui/MNotebookListItemWidget.h"
#include "QvernoteAPI.h"
#include <QGraphicsPixmapItem>
#include <MAbstractCellCreator>
#include <MApplication>

class QNotebookListItemWidget : public MAbstractCellCreator<MNotebookListItemWidget>
{
    //Q_OBJECT

public:
    QNotebookListItemWidget(bool isDeleteMode = false, int noteCount = -1)
        : m_bDeleteMode(isDeleteMode), m_nNoteCount(noteCount)
    {
        setCellViewType((isDeleteMode)? "Multi" : "Single");
    }

    ~QNotebookListItemWidget() { }

    void    updateCell (const QModelIndex &index, MWidget *cell) const
    {
        QVariant checkData = index.data(Qt::CheckStateRole);
        Qt::CheckState checkState = (Qt::CheckState)checkData.value<int>();
        MNotebookListItemWidget* w = (MNotebookListItemWidget*)cell;

        if(checkState == Qt::Checked)
        {
            w->lDeleteMark->setPixmap(QPixmap(":/IconAdd/widgets_tickmark_list.png"));
        }
        else
            w->lDeleteMark->setPixmap(QPixmap());

        QVariant data = index.data(Qt::DisplayRole);
        int notebookptr = data.value<int>();
        Notebook* notebook = (Notebook*)(int*)notebookptr;
        QvernoteAPI* api = QvernoteAPI::Instance();

        if(notebook->defaultNotebook)
        {
            w->lNotebookIcon->setVisible(true);
            w->lNotebookIcon->setPixmap(QPixmap(":/IconAdd/star-32.png"));
        }
        else
        {
            w->lNotebookIcon->setVisible(false);
        }

        QStringList blacklistedNotebooks = QvernoteSettings::Instance()->getBlacklistedNotebooks().split(',');
        if(blacklistedNotebooks.contains(QString::fromStdString(notebook->guid)) && !notebook->guid.empty())
        {
                qDebug() << "Notebook is blacklisted: " << notebook->name.c_str();
                w->lNotebookIcon->setVisible(true);
                w->lNotebookIcon->setPixmap(QPixmap(":/IconAdd/general_presence_offline.png"));
        }

        w->lNotebookName->setText(QString::fromUtf8(notebook->name.c_str()));

        w->lNotesCount->setText(QString::number((m_nNoteCount == -1)? api->getNotebookContentsCount(*notebook) : m_nNoteCount, 10));


        Tag tag;
        bool hasNextTag;
        QString tagList;
        for(hasNextTag = api->getFirstNotebookTag(notebook->name, tag); hasNextTag; hasNextTag = api->getNextNotebookTag(notebook->name, tag))
        {
                tagList += QString::fromUtf8(tag.name.c_str()) + " ";
        }

        if(tagList.size() == 0)
        {
                w->lNotebookTags->setText(QApplication::trUtf8("No tags"));
        }
        else
        {
                w->lNotebookTags->setText(tagList);
        }

    }

private:
    bool	m_bDeleteMode;
    int		m_nNoteCount;
};

#endif // QNOTEBOOKLISTITEMWIDGET_H
